# -*- encoding: utf-8 -*-
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.utils.translation import ugettext_lazy as _

from django.views.decorators.cache import cache_page

from forms import ContactForm
from models import Contact

@cache_page(60 * 15)
def contact(request):
    contacts = request.section.contacts.order_by('email')

    mails = [c.email for c in contacts]

    message = None

    contact_form = ContactForm()

    if request.method == "POST":
        contact_form = ContactForm(request.POST)

        if contact_form.is_valid():
            contact_form.send(mails)
            message = _(u'Message envoyé')
            contact_form = ContactForm()
        else:
            message = _(u'Le mail n\'a pu être envoyé')


    return render_to_response('contact/contact.html',
                              {'form': contact_form,
                               'message': message,},
                              context_instance=RequestContext(request))
